//
//  GroupViewController.m
//  高仿萝卜书摘
//
//  Created by 谢俊逸 on 2016/11/23.
//  Copyright © 2016年 谢俊逸. All rights reserved.
//

#import "GroupViewController.h"
#import "XJYHeader.h"
#import "GroupCollectionViewCell.h"
#import "GroupCollectionFootView.h"
#import "MyHeaderView.h"
#import "GroupTitleLabelView.h"

#import "GroupAddCollectionViewCell.h"
#import "GroupCreationCollectionViewCell.h"


static NSString *const GroupCollectionCellID = @"GroupCollectionCellID";
static NSString *const GroupCollectionAddCellID = @"GroupCollectionAddCellID";

static NSString *const GroupCollectionCreatCellID = @"GroupCollectionCreatCellID";

static NSString *const GroupCollectionHeaderSupplementaryViewID = @"GroupCollectionHeaderSupplementaryViewID";
static NSString *const GroupCollectionFooterSupplementaryViewID = @"GroupCollectionFooterSupplementaryViewID";



@interface GroupViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
@property (nonatomic,strong) UICollectionView *collectionView;


@property (nonatomic,strong) NSArray *itemArray;
@end

@implementation GroupViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUIView];
    [self setupNavgationItem];

}




- (void)setupUIView{

    UICollectionViewFlowLayout *flowLayout = ({
        flowLayout = [[UICollectionViewFlowLayout alloc]init];
        flowLayout.itemSize = [FrameAutoScaleLFL CGSizeLFLMakeWidth:160 hight:240];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        CGFloat margin = 15;
        flowLayout.sectionInset = UIEdgeInsetsMake(margin, margin, margin, margin);
        flowLayout.minimumInteritemSpacing = 10;
        flowLayout.minimumLineSpacing = 20;
#pragma mark -- 头尾部大小设置
        //设置头部并给定大小
        //[flowLayout setHeaderReferenceSize:CGSizeMake(ScreenW, 50)];
        //设置尾部并给定大小
        [flowLayout setFooterReferenceSize:CGSizeMake(ScreenW, 30)];
        //flowLayout.footerReferenceSize
        flowLayout;
    });
    
    
    UICollectionView *collectionView = ({
        collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH) collectionViewLayout:flowLayout];
        collectionView.backgroundColor = ColorWithRGBA(250, 250, 250, 1);
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        collectionView.dataSource = self;
        //当界面内容不超过界面大小时不会滑动，加上面这句话就能滑动了
        collectionView.alwaysBounceVertical = YES;
        [self.view addSubview:collectionView];
        collectionView;
    });
    
#pragma mark 注册collectionViewCell
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([GroupCollectionViewCell class])  bundle:nil] forCellWithReuseIdentifier:GroupCollectionCellID];
#pragma mark 注册GroupCollectionFooterSupplementaryViewID
    [collectionView registerClass:[GroupCollectionFootView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:GroupCollectionFooterSupplementaryViewID];
#pragma mark 注册GroupCollectionHeaderSupplementaryViewID
    [collectionView registerClass:[MyHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:GroupCollectionHeaderSupplementaryViewID];

#pragma mark 注册AddCreatCell
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([GroupAddCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:GroupCollectionAddCellID];
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([GroupCreationCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:GroupCollectionCreatCellID];
}


- (void)setupNavgationItem{
    
    GroupTitleLabelView *titleLabel = [[GroupTitleLabelView alloc]init];
    self.navigationItem.titleView = titleLabel;
    
    
    
    UIBarButtonItem *leftBarItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(leftBarButtonClicked:)];
    leftBarItem.tintColor = [UIColor lightGrayColor];
    self.navigationItem.leftBarButtonItem = leftBarItem;
    
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(rightBarButtonClicked:)];
    rightBarItem.tintColor = [UIColor lightGrayColor];
    self.navigationItem.rightBarButtonItem = rightBarItem;
}


#pragma mark CollectionViewDateSource
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    //new and search card
    //先把基本的界面做了
    return _itemArray.count + 2 + 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    //
        //倒数第一个倒数第二个放creatCell 和 addCell
        if (indexPath.row == [collectionView numberOfItemsInSection:0] - 1) {
            GroupAddCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:GroupCollectionAddCellID forIndexPath:indexPath];
            return cell;
            
        }
        if (indexPath.row == [collectionView numberOfItemsInSection:0] - 2){
            GroupCreationCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:GroupCollectionCreatCellID forIndexPath:indexPath];
            return cell;
        }
    
    GroupCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:GroupCollectionCellID forIndexPath:indexPath];
    
    return cell;
}


//设置头尾部内容
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    UICollectionReusableView *reusableView = nil;

    if (kind == UICollectionElementKindSectionFooter){
        GroupCollectionFootView *footerV = (GroupCollectionFootView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:GroupCollectionFooterSupplementaryViewID forIndexPath:indexPath];
        
        reusableView = footerV;
    }
    return reusableView;
}
#pragma mark BarButtonItemAction

- (void)leftBarButtonClicked:(UIBarButtonItem *)barButtonItem{
    NSLog(@"click leftBarButtonItem");
}


- (void)rightBarButtonClicked:(UIBarButtonItem *)barButtonItem{
    NSLog(@"click rightBarButtonItem");

}


@end
